const MAX_TIMES = 50

$(document).ready(function() {
  runCleaner()

  $('body').on('click', () => {
    runCleaner()
  })

  // 监听从百度搜索首页跳转至搜索结果页
  chrome.runtime.onMessage.addListener(
    function(message, sender, sendResponse) {
      runCleaner()
    }
  )
})

function runCleaner() {
  getCleanerStorage(function(items) {
    const { isOpenCleaner, isOpenDomain } = items

    if (isOpenCleaner || isOpenDomain) {
      cleanLoop()
    } else {
      stopCleanLoop()
    }
  })
}

function getCleanerStorage(cb) {
  chrome.storage.sync.get({
    isOpenCleaner: true,
    isOpenDomain: false,
    domainList: []
  }, function (items) {
    cb && cb(items)
  })
}

function cleanLoop() {
  cleanOnce()

  let times = 0

  let timer = setInterval(() => {
    cleanOnce()

    times++
    if (times >= MAX_TIMES) {
      times = 0

      stopCleanLoop(timer)
    }
  }, 200)
}

function stopCleanLoop(timer) {
  if (timer) {
    clearInterval(timer)
    timer = null
  }
}

function cleanOnce() {
  getCleanerStorage(function(items) {
    const { isOpenCleaner, isOpenDomain } = items

    if (isOpenCleaner) {
      cleanSearchIndex()

      cleanSearchList()
    }

    if (isOpenDomain) {
      cleanSearchListByDomain()
    }
  })
}

function cleanSearchIndex() {
  const topLeft = $('#s-top-left')
  const mancardMain = $('#s_mancard_main')

  topLeft && topLeft.remove()
  mancardMain && mancardMain.remove()
}

function cleanSearchList() {
  const contentLeft = $('#content_left')
  const cContainer = contentLeft.children()
  const cSpan9 = $('.new-pmd .c-span9')
  const contentRight = $('#content_right')

  // 修改搜索列表的样式
  contentLeft && contentLeft.css({
    width: '100%'
  })
  cContainer && cContainer.css({
    width: '100%'
  })
  cSpan9 && cSpan9.css({
    width: '80%'
  })

  // 清除搜索列表中的广告词条
  if (cContainer) {
    cContainer.each(function(i) {
      const adNames = ['广告', '推广']

      const ad = $(this).find('.ec-tuiguang')
      if (ad && ad.get(0)) {
        $(this).remove()
      }

      const links = $(this).find('a')
      if (links) {
        links.each(function(j) {
          if (adNames.includes($(links[j]).text())) {
            $(cContainer[i]).remove()
          }
        })
      }
    })
  }

  // 清除右侧广告
  contentRight && contentRight.remove()
}

function cleanSearchListByDomain() {
  getCleanerStorage(function(items) {
    const domainList = items.domainList.filter(item => !!item.is_open).map(item => item.url)

    if (domainList.length) {
      const contentLeft = $('#content_left')
      const cContainer = contentLeft.children()

      if (cContainer) {
        cContainer.each(function (i) {
          const link = $(this).attr('mu')
          let pass = false

          if (link) {
            for (let i = 0; i < domainList.length; i++) {
              if (link.indexOf(domainList[i]) > -1) {
                pass = true
                break
              }
            }
          }

          if (!pass) {
            $(this).remove()
          }
        })
      }
    }
  })
}